﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.SYN;
using DaInterFace.SYN.E01;
using System.Reflection;
namespace E02
{
    /// <summary>
    /// 销售授信
    /// </summary>
    public class A09 : ISYNChildGet
    {
        public void Code(ISYN handler, ref CodeResult result, SYNRight user, SerialType st, String baseNo, String billId)
        {
            try
            {
                result.Code = "E02.A09";
                List<E02A09P> ht = new List<E02A09P>();
                SqlParameter[] sps = new SqlParameter[1];
                sps[0] = new SqlParameter("@FID", SqlDbType.NVarChar, 50);
                sps[0].Value = billId;
                string sql = " select a.FID BillId,FBillNo Number,case when FTime = '' then '1990-01-01 00:00:00' when FTime IS null then '1990-01-01 00:00:00' else FTime end CreatTime," +
                             " case when FTime1 = '' then '1990-01-01 00:00:00' when FTime1 IS null then '1990-01-01 00:00:00' else FTime1 end CheckTime,b.F_102 CusName," +
                             " FDecimal CreditMoney,FDecimal1 OffsetMoney,FNOTE Remark,FID_SRC SrcID,FClassID_SRC SrcBillType,d.FName Checker,e.FName Biller " +
                             " ,(select FText5 from CardServer.dbo.KEW_DataBaseEntry where FNumber = " + baseNo + " ) FiUnitID from KEW_SalesCredit  a" +
                             " left join t_Organization b on a.FBASE = b.FItemID " +
                             " left join t_User d on d.FUserID = a.FUser and a.FUser <> 0 " +
                             " left join t_User e on e.FUserID = a.FBiller and a.FBiller <> 0 where FID = @FID";
                Recordset rs = handler.Data.ExeProcedure(baseNo, sql, sps);
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows.Count == 1)
                    {
                        DataRow row = rs.Data.Tables[0].Rows[0];
                        if (row["CusName"] == null || row["CusName"].ToString().Equals(""))
                        {
                            result.Result = EnumResult.None;
                            result.Message = String.Format("销售授信数据提取错误:{0}", "请先配置基础资料客户对应关系！编号：" + row["Number"].ToString());
                            return;
                        }
                        E02A09P e02A09P = new E02A09P();
                        for (int i = 0; i < rs.Data.Tables[0].Columns.Count; i++)
                        {
                            DataColumn dc = rs.Data.Tables[0].Columns[i];
                            ReflectUtil.setValue(e02A09P, dc.ColumnName, row[dc.ColumnName].ToString());
                        }

                        ht.Add(e02A09P);
                        result.Content = handler.Json.ClassToJson(ht);
                    }
                    else
                    {
                        result.Result = EnumResult.None;
                    }
                }
                else
                {
                    result.Result = EnumResult.None;
                    result.Message = String.Format("销售授信数据提取错误:{0}", rs.Message);
                }
            }
            catch (Exception ex)
            {
                result.Result = EnumResult.None;
                result.Message = String.Format("销售授信提取错误:{0}", ex.Message);
            }
        }

    }

    /// <summary>
    /// 销售授信
    /// </summary>
    public class E02A09P
    {
        public String BillId = String.Empty; //单据ID
        /// <summary>
        /// 账套编号
        /// </summary>
        public String BaseNo;
        /// <summary>
        /// 三方内码
        /// </summary>
        public String ID;
        /// <summary>
        /// 三方编号
        /// </summary>
        public String Number;
        /// <summary>
        /// 三方名称
        /// </summary>
        //public String Name;
        /// <summary>
        /// 制单时间
        /// </summary>
        public String CreatTime;
        /// <summary>
        /// 审核时间
        /// </summary>
        public String CheckTime;
        /// <summary>
        /// 付款单位
        /// </summary>
        public String CusName;
        /// <summary>
        /// 授信金额
        /// </summary>
        public Decimal CreditMoney;
        /// <summary>
        /// 冲抵金额
        /// </summary>
        public Decimal OffsetMoney;
        /// <summary>
        /// 备注
        /// </summary>
        public String Remark;        
        /// <summary>
        /// 源单内码
        /// </summary>
        public String SrcID;
        /// <summary>
        /// 源单类型
        /// </summary>
        public String SrcBillType;
        /// <summary>
        /// 审核人
        /// </summary>
        public String Checker;
        /// <summary>
        /// 制单人
        /// </summary>
        public String Biller;
        
        /// <summary>
        /// EAS财务组织
        /// </summary>
        public String FiUnitID;
    }
}
